# -*- coding: utf-8 -*-

"""剑指 Offer II 009. 乘积小于 K 的子数组
给定一个正整数数组 nums和整数 k ，请找出该数组内乘积小于 k 的连续的子数组的个数。

示例 1:
输入: nums = [10,5,2,6], k = 100
输出: 8
解释: 8 个乘积小于 100 的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。
需要注意的是 [10,5,2] 并不是乘积小于100的子数组。

示例 2:
输入: nums = [1,2,3], k = 0
输出: 0

提示: 
1 <= nums.length <= 3 * 10^4
1 <= nums[i] <= 1000
0 <= k <= 10^6"""

class Solution:
    def numSubarrayProductLessThanK(self, nums, k) -> int:
        multi, n, ln = nums[0], 0, len(nums)-1

        tail = 0
        for head in range(len(nums)):
            if tail < head:
                tail = head
                multi *= nums[tail]
            while True:
                if tail < ln and multi < k and multi*nums[tail+1] < k:
                    tail += 1
                    multi *= nums[tail]
                else:
                    break
            if nums[head] < k:
                print(head, tail, nums[head],nums[tail])
                n += (tail-head+1)
            multi /= nums[head]
        return n

if __name__ == '__main__':
    so = Solution()
    # print(so.numSubarrayProductLessThanK([10,5,2,6], 100))
    # print(so.numSubarrayProductLessThanK([1,2,3], 0))
    print(so.numSubarrayProductLessThanK([10,9,10,4,3,8,3,3,6,2,10,10,9,3], 19))
